package com.fang.transfer.file.common.model;


import com.google.gson.annotations.Expose;
import lombok.Data;

import java.util.ArrayList;
import java.util.BitSet;

@Data
public class TransferFileRequest {

    private String id;
    private String name;
    /**
     * 文件大小
     */
    private Long length;
    /**
     * udp size
     */
    public int udpPackageSize = 35507;
    private String path;


    /**
     * 请求状态  1 请求同意接收  2 UDP地址获取  3 客户端打洞(31:发送方打洞成功,32:接收方打洞成功)   4 开始传输  5 单次发送完成,检测文件完整性
     */
    private volatile int status;

    /**
     * 1 发送  2接收
     */
    private String role;
    private User receiveUser;
    private User sendUser;

    private long curCount;


    @Expose(serialize = false, deserialize = false)
    private BitSet curData;


    private ArrayList<Integer> reSendIndex;

    public int getMaxPackage() {
        if (length == null) {
            throw new RuntimeException("未初始化文件大小");
        }
        int result = (length.intValue() - 1) / (this.udpPackageSize - 2) + 1;
        return result;
    }

    public synchronized void addCurCount(Integer index) {
        boolean b = this.curData.get(index.intValue());
        if (!b) {
            curData.set(index.intValue(), true);
            this.curCount++;
        }
    }

    public void initParam() {
        this.curData = new BitSet(getMaxPackage());
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
